// pages/hotel/hotelDetail/hotelDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '', //日期
    dateStart: '', //开始日期
    dateEnd: '', //结束日期
    weekdayStart: '', //开始周几
    weekdayEnd: '', //结束周几
    show: false, //日期组件是否展示
    btn11: 0, //记录多选按钮状态

    noAddressCollect: '/images/not_collect.png', //是否收藏
    addressCollect: '/images/collect.png',
    collect: 0,
    hotelDetail: '',
    roomType: [],
    userId: 0,
    hotelId: 0,
    collectList: [],
    opId: 0,
    hotelEvaluationList: [],
    adult: 1,
    child: 0,
  },

  collect1() {
    if (this.data.collect == 0) {
      this.setData({
        collect: 1
      })
      //执行删除
      this.getCollectNoIf()
    } else {
      this.setData({
        collect: 0
      })
      //执行新增
      this.addCollect()
    }
  },

  // 多选按钮事件
  btn1(e) {
    console.log(e.target.dataset.id)
    this.setData({
      btn11: this.data.btn11 == e.target.dataset.id ? 0 : e.target.dataset.id
    })
  },

  /**
   * 日期改变触发事件
   */
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}月${date.getDate()}日`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    var weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    var currentStart = start.getDay(); //指定开始日期
    var currentEnd = end.getDay(); //指定结束日期
    this.setData({
      show: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      dateStart: `${this.formatDate(start)}`,
      dateEnd: `${this.formatDate(end)}`,
      weekdayStart: weeks[currentStart],
      weekdayEnd: weeks[currentEnd],
    });
  },
  getCollectNoIf() {
    wx.request({
      url: 'http://localhost:8080/hotel/hotelCollect/list',
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelId: this.data.hotelId,
        userId: this.data.userId
      },
      success: (resp) => {
        console.log("co", resp)
        this.setData({
          collectList: resp.data.rows,
        })
        this.delCollect()
      }
    })
  },
  // 查询收藏
  getCollect() {
    wx.request({
      url: 'http://localhost:8080/hotel/hotelCollect/list',
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelId: this.data.hotelId,
        userId: this.data.userId
      },
      success: (resp) => {
        console.log("co", resp)
        if (resp.data.rows.length == 0) {
          this.setData({
            collect: 1
          })
        }
        this.setData({
          collectList: resp.data.rows,
        })
      }
    })
  },
  // 删除收藏
  delCollect() {
    // collectList[0].id
    wx.request({
      url: `http://localhost:8080/hotel/hotelCollect/${this.data.collectList[0].id}`,
      method: 'delete',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log("de", resp)
      }
    })
  },
  // 新增收藏
  addCollect() {
    wx.request({
      url: 'http://localhost:8080/hotel/hotelCollect',
      method: 'post',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelId: this.data.hotelId,
        userId: this.data.userId
      },
      success: (resp) => {
        console.log("add", resp)
      }
    })
  },
  getHotelDetilById() {
    wx.request({
      url: `http://localhost:8080/hotel/hotel/${this.data.opId}`,
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log("resp", resp)
        this.setData({
          hotelDetail: resp.data.data,
          hotelId: resp.data.data.id
        })
        console.log('赋值', this.data.hotelDetail)
        // 查询用户id
        var loginfo = getApp().globalData.loginfo;
        console.log(loginfo)
        this.setData({
          userId: loginfo.userId
        })
        this.getCollect()
        this.getEevaluation()
      }
    })
  },
  // 根据酒店id查房型 /hotel/${options.id}
  getRoomTypeByHotelId() {
    wx.request({
      url: `http://localhost:8080/hotel/hotelRoom/hotel/${this.data.opId}`,
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          roomType: resp.data
        })
        console.log("rt", this.data.roomType)
      }
    })
  },
  // 根据房型id和酒店id查询
  getRoomByTypeIdAndHotelId() {

  },
  getEevaluation(options) {
    wx.request({
      url: 'http://localhost:8080/hotel/hotelEvaluation/hotels/' + this.data.hotelId,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          hotelEvaluationList: resp.data.data
        })
        console.log("pl", resp.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("cz", options.id)
    this.setData({
      opId: options.id
    })
    this.getHotelDetilById()
    // 根据酒店id查房型 /hotel/${options.id}
    this.getRoomTypeByHotelId()



    // 展示初始日期
    var date = new Date();
    var currentDate = date.getDay(); //获取存储当前日期
    var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    this.setData({
      dateStart: date.getMonth() + 1 + '月' + date.getDate() + '日',
      dateEnd: date.getMonth() + 1 + '月' + (date.getDate() + 1) + '日',
      weekdayStart: weekday[currentDate],
      weekdayEnd: weekday[currentDate + 1],
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})